

jQuery(function ($) {
    var user = JSON.parse(localStorage.getItem('user_info')) || {};
    var id = user.id;
    $('.ex3').zoom({ on:'click' });	
    $('.buy_now').click(function () {
        var goodsId = $(this).attr('data-goodsId');
        var count =  parseInt($('.inpt_num').val());
        window.location.href = '/goods/other_order.html?goods_id='+ goodsId+'&'+'count='+ count
    });
    $('.memer').hide();
    if (!id) {
        $('.memer').show();
        $('.memer').click(function () {
            window.location.href = "/login/login.html";
        });
    };
 
    $('.close_mading').click(function () {
        $('#mask').hide();
        $('#dialog').hide()

    });
    var goods_id = window.location.href.split('-')[1].split('.')[0];
    var goodsType = {
        userId: id ? id : 0,
        goodsId: goods_id
    };
    var stock;
    send_ajax('get', '/api/goods/status', goodsType, function callback(data) {
        if (data.code == 200&&data.data) {
                var result = data.data;
                 stock = result.stock;
                var unitName = result.unitName;
                var url = result.url;
                  if(result.special_flag){
                    $('.btn').hide(); 
                    $('.btn_made').show();  
                    $('.item_buy').hide();
                  }
                  if(url=='tudi'){
                    $('.item_stock .price_msg').html('土地面积');
                    $('.item_batch').hide();  
                  }

                  if(result.stock>0){
                      $('.stock_num').html(stock+unitName);
                  }else{
                    $('.stock_num').html(0); 
                    $('.btn_made').show();  
                    $('.item_buy').hide();
                  }


            
        }
    });
     //咨询、字画问答切换
     $('.information span').click(function(){
        var index = $(this).index();
        $(this).addClass("ask_color").siblings().removeClass("ask_color");
        $(".information_answer").eq(index).addClass('open').siblings().removeClass("open");;
    });
    //是否收藏了此商品
    isStore();
    function isStore() {
        if (id) {
            var param = {
                "GoodsCollect": {
                    "goods_id": goods_id,
                    "user_id": id
                }
            };
            send_ajax('post', '/head', param, function callback(res) {
                if (res.code == 200 && res.GoodsCollect.count > 0) {
                    $('.storage_shop').find('i').addClass('selected').removeClass('ico_shop');
                }
            });
        }
    }
    // 收藏商品
    $('.storage_shop').click(function () {
        if (id) {
            var is_select = $('.storage_shop').find('i').hasClass('selected');
            var goods_id = $(this).attr('data-goods-id');
            var param = {
                "GoodsCollect": {
                    "goods_id": goods_id,
                    "user_id": id
                },
                "tag": "GoodsCollect"
            };
            var storeId_param = {
                "GoodsCollect": {
                    "user_id": id,
                    "goods_id": goods_id
                }
            };
            send_ajax('post', '/head', storeId_param, function callback(data) {
                if (data.code == 200) {
                    send_ajax('post', data.GoodsCollect.count > 0 ? '/delete' : '/post', param, function callback(res) {
                        if (res.code == 200) {
                            if (is_select) {
                                $('.storage_shop').find('i').removeClass('selected').addClass('ico_shop');
                            } else {
                                $('.storage_shop').find('i').addClass('selected').removeClass('ico_shop');
                            }
                        }
                    });
                }
            });
        } else {
            alert('请登录后再进行收藏！');
        }
    });
    $('.contact').click(function(){
        $('#mask').show();
        $('#dialog').show()
    })
    $('.inpt_num').on('input',function () {
        var value =  $(this).val();
        this.value = value.replace(/[^\d]/g, '').replace(/^0{1,}/g,'');
        if(parseInt(value)>stock){
            this.value = stock;
        };
    });
    $(".inpt_num").blur(function () {
        var value =  $(this).val();
        if(!value){
            this.value = 1; 
        }
    });
    $('.inpt_num').val(1)
    $('.add').click(function(){
        var number = $('.inpt_num').val();
        if(number < stock){
            $('.inpt_num').val(parseInt($('.inpt_num').val())+1);
        }
    })
    $('.reduce').click(function(){
        var num =  parseInt($('.inpt_num').val())-1;
        if(num>=1){
           $('.inpt_num').val(num); 
        }
        
    })
    var phone =  $('.artist .contact').attr('data-phone');
    $('.phone').html('+86'+'&nbsp'+ +phone)
    //分享插件
    window._bd_share_config = {
        "common": {
            "bdSnsKey": {},
            "bdText": "",
            "bdMini": "2",
            "bdMiniList": false,
            "bdPic": "",
            "bdStyle": "0",
            "bdSize": "16"
        },
        "share": {}
    };
    with(document) 0[(getElementsByTagName('head')[0] || body)
        .appendChild(createElement('script')).src =
        'https://static.yczihua.com/static/api/js/share.js?v=89860593.js?cdnversion=' + ~(-new Date() / 36e5)];
});